package com.tiennt.android.chinesechess.ui;

import com.tiennt.android.chinesechess.models.BitmapHolder;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.Log;

public class UIPainter {

	// Debug purpose.
	private static boolean DBG = true;
	private static String TAG = "UIPainter";
	public static final int LEFT = 1;
	public static final int RIGHT = 2;

	public static void drawGameBackground(Canvas canvas, Paint paint) {

		paint.setStrokeWidth(5);
		paint.setColor(Color.parseColor("#A4433A"));
		paint.setStyle(Style.STROKE);
		paint.setAntiAlias(true);

		// Hai duong ngang tren duoi cung.
		canvas.drawLine(38, 38, 682, 38, paint);
		canvas.drawLine(38, 762, 682, 762, paint);

		paint.setStrokeWidth(3);
		for (int i = 0; i < 10; i++) {
			canvas.drawLine(40, i * 80 + 40, 680, i * 80 + 40, paint);
		}

		for (int i = 0; i < 7; i++) {
			canvas.drawLine(120 + i * 80, 40, 120 + i * 80, 360, paint);
			canvas.drawLine(120 + i * 80, 440, 120 + i * 80, 760, paint);
		}

		// Hai duong doc ben hong.
		canvas.drawLine(40, 40, 40, 760, paint);
		canvas.drawLine(680, 40, 680, 760, paint);

		// 2 Duong cheo tren
		canvas.drawLine(280, 40, 440, 200, paint);
		canvas.drawLine(280, 200, 440, 40, paint);

		// 2 duong cheo duoi
		canvas.drawLine(280, 600, 440, 760, paint);
		canvas.drawLine(280, 760, 440, 600, paint);

		// Quan tot dung o giua
		drawTOTPosition1(canvas, paint, 120, 200);
		drawTOTPosition1(canvas, paint, 200, 280);
		drawTOTPosition1(canvas, paint, 360, 280);
		drawTOTPosition1(canvas, paint, 520, 280);
		drawTOTPosition1(canvas, paint, 600, 200);

		drawTOTPosition1(canvas, paint, 120, 600);
		drawTOTPosition1(canvas, paint, 200, 520);
		drawTOTPosition1(canvas, paint, 360, 520);
		drawTOTPosition1(canvas, paint, 520, 520);
		drawTOTPosition1(canvas, paint, 600, 600);

		drawTOTPosition2(canvas, paint, 40, 280, 1);
		drawTOTPosition2(canvas, paint, 40, 520, 1);

		drawTOTPosition2(canvas, paint, 680, 280, 2);
		drawTOTPosition2(canvas, paint, 680, 520, 2);
	}

	private static void drawTOTPosition1(Canvas canvas, Paint paint,
			int centerX, int centerY) {
		paint.setColor(Color.parseColor("#A4433A"));
		paint.setStyle(Style.FILL);
		paint.setAntiAlias(true);

		// Ngang
		canvas.drawRect(centerX - 8, centerY - 4, centerX + 8, centerY + 4,
				paint);

		// Doc
		canvas.drawRect(centerX - 4, centerY - 8, centerX + 4, centerY + 8,
				paint);

	}

	private static void drawTOTPosition2(Canvas canvas, Paint paint,
			int centerX, int centerY, int leftright) {
		paint.setColor(Color.parseColor("#A4433A"));
		paint.setStyle(Style.FILL);
		paint.setAntiAlias(true);
		// LEFT
		if (leftright == LEFT) {
			canvas.drawRect(centerX, centerY - 4, centerX + 8, centerY + 4,
					paint);
			canvas.drawRect(centerX, centerY - 8, centerX + 4, centerY + 8,
					paint);
		} else if (leftright == RIGHT) {
			// Ngang
			canvas.drawRect(centerX - 8, centerY - 4, centerX, centerY + 4,
					paint);

			// Doc
			canvas.drawRect(centerX - 4, centerY - 8, centerX, centerY + 8,
					paint);
		}

	}

	public static void drawQuanCo(Canvas canvas, int x, int y, Bitmap bitmap) {
		int realPosX = x * 80 + 40;
		int realPosY = y * 80 + 40;

		canvas.drawBitmap(bitmap,
				new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
				new Rect(realPosX - 35, realPosY - 35, realPosX + 35,
						realPosY + 35), null);
	}

	public static void log(final String message) {
		if (DBG) {
			Log.d(TAG, message);
		}
	}

	public static void drawBanCoUpStart(Canvas canvas, Paint paint,
			Bitmap bitmap) {
		for (int i = 0; i < 9; i++) {
			drawQuanCo(canvas, i, 0, bitmap);
			drawQuanCo(canvas, i, 9, bitmap);
		}

		drawQuanCo(canvas, 1, 2, bitmap);
		drawQuanCo(canvas, 7, 2, bitmap);

		drawQuanCo(canvas, 0, 3, bitmap);
		drawQuanCo(canvas, 2, 3, bitmap);
		drawQuanCo(canvas, 4, 3, bitmap);
		drawQuanCo(canvas, 6, 3, bitmap);
		drawQuanCo(canvas, 8, 3, bitmap);

		drawQuanCo(canvas, 1, 7, bitmap);
		drawQuanCo(canvas, 7, 7, bitmap);

		drawQuanCo(canvas, 0, 6, bitmap);
		drawQuanCo(canvas, 2, 6, bitmap);
		drawQuanCo(canvas, 4, 6, bitmap);
		drawQuanCo(canvas, 6, 6, bitmap);
		drawQuanCo(canvas, 8, 6, bitmap);
	}

	public static void drawBanCoThuongStart(Canvas canvas, Paint paint,
			BitmapHolder holder) {

		// XE
		drawQuanCo(canvas, 0, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.XE_DO_NORMAL));
		drawQuanCo(canvas, 8, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.XE_DO_NORMAL));
		drawQuanCo(canvas, 0, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.XE_DEN_NORMAL));
		drawQuanCo(canvas, 8, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.XE_DEN_NORMAL));

		// MA
		drawQuanCo(canvas, 1, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.MA_DO_NORMAL));
		drawQuanCo(canvas, 1, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.MA_DEN_NORMAL));
		drawQuanCo(canvas, 7, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.MA_DO_NORMAL));
		drawQuanCo(canvas, 7, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.MA_DEN_NORMAL));

		// TINH
		drawQuanCo(canvas, 2, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.TINH_DO_NORMAL));
		drawQuanCo(canvas, 2, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.TINH_DEN_NORMAL));
		drawQuanCo(canvas, 6, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.TINH_DO_NORMAL));
		drawQuanCo(canvas, 6, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.TINH_DEN_NORMAL));

		// SY
		drawQuanCo(canvas, 3, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.SY_DO_NORMAL));
		drawQuanCo(canvas, 3, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.SY_DEN_NORMAL));
		drawQuanCo(canvas, 5, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.SY_DO_NORMAL));
		drawQuanCo(canvas, 5, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.SY_DEN_NORMAL));

		// TUONG
		drawQuanCo(canvas, 4, 0,
				holder.getBitmap(BitmapHolder.TenQuanCo.TUONG_DO_NORMAL));
		drawQuanCo(canvas, 4, 9,
				holder.getBitmap(BitmapHolder.TenQuanCo.TUONG_DEN_NORMAL));

		// PHAO
		drawQuanCo(canvas, 1, 2,
				holder.getBitmap(BitmapHolder.TenQuanCo.PHAO_DO_NORMAL));
		drawQuanCo(canvas, 7, 2,
				holder.getBitmap(BitmapHolder.TenQuanCo.PHAO_DO_NORMAL));

		// TOT
		drawQuanCo(canvas, 0, 3,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DO_NORMAL));
		drawQuanCo(canvas, 2, 3,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DO_NORMAL));
		drawQuanCo(canvas, 4, 3,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DO_NORMAL));
		drawQuanCo(canvas, 6, 3,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DO_NORMAL));
		drawQuanCo(canvas, 8, 3,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DO_NORMAL));

		drawQuanCo(canvas, 1, 7,
				holder.getBitmap(BitmapHolder.TenQuanCo.PHAO_DEN_NORMAL));
		drawQuanCo(canvas, 7, 7,
				holder.getBitmap(BitmapHolder.TenQuanCo.PHAO_DEN_NORMAL));

		drawQuanCo(canvas, 0, 6,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DEN_NORMAL));
		drawQuanCo(canvas, 2, 6,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DEN_NORMAL));
		drawQuanCo(canvas, 4, 6,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DEN_NORMAL));
		drawQuanCo(canvas, 6, 6,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DEN_NORMAL));
		drawQuanCo(canvas, 8, 6,
				holder.getBitmap(BitmapHolder.TenQuanCo.TOT_DEN_NORMAL));
	}

	public static void drawHint(Canvas canvas, Paint paint, int x, int y) {
		paint.setColor(Color.argb(150, 255, 255, 255));

		int realPosX = x * 80 + 40;
		int realPosY = y * 80 + 40;

		canvas.drawCircle(realPosX, realPosY, 20, paint);
	}
}
